package com.adtec.base.cloud.baseservice.rbac.system.controller;


import com.adtec.base.cloud.baseservice.rbac.system.model.RoleCheckedIds;
import com.adtec.base.cloud.baseservice.rbac.system.model.SysApiNode;
import com.adtec.base.cloud.baseservice.rbac.system.service.SysApiService;
import com.adtec.base.cloud.commons.exception.AjaxResponse;
import com.adtec.base.cloud.persistence.system.model.SysApi;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sysapi")
@Tag(name = "sysapi", description = "接口管理")
public class SysApiController {

    @Resource
    private SysApiService sysapiService;

    //接口管理:查询
    @PostMapping(value = "/tree")
    @Operation(description = "接口管理:查询")
    @Parameters({
            @Parameter(name = "apiNameLike", description = "接口名称(模糊查询)"),
            @Parameter(name = "apiStatus", description = "接口状态"),
    })
    public List<SysApiNode> tree(@RequestParam(name = "apiNameLike", required = false) String apiNameLike,
                                 @RequestParam(name = "apiStatus", required = false) Boolean apiStatus) {

        return sysapiService.getApiTree(apiNameLike, apiStatus);
    }

    //接口管理:修改
    @PostMapping(value = "/update")
    @Operation(description = "接口管理:修改")
    public AjaxResponse update(@RequestBody SysApi sysApi) {
        sysapiService.updateApi(sysApi);
        return AjaxResponse.success("更新接口配置成功！");
    }

    //接口管理:新增
    @PostMapping(value = "/add")
    @Operation(description = "接口管理:新增")
    public AjaxResponse add(@RequestBody SysApi sysApi) {
        sysapiService.addApi(sysApi);
        return AjaxResponse.success("新增接口配置成功！");
    }

    //接口管理:删除
    @PostMapping(value = "/delete")
    @Operation(description = "接口管理:删除")
    public AjaxResponse delete(@RequestBody SysApi sysApi) {
        sysapiService.deleteApi(sysApi);
        return AjaxResponse.success("删除接口配置成功!");
    }

    //角色管理：API树展示（勾选项、展开项）
    @PostMapping(value = "/checkedtree")
    @Operation(description = "角色管理：API树展示（勾选项、展开项）")
    @Parameters({
            @Parameter(name = "roleId", description = "角色ID"),
    })
    public Map<String, Object> checkedtree(@RequestParam(name = "roleId") Long roleId) {
        Map<String, Object> ret = new HashMap<>();
        ret.put("tree", sysapiService.getApiTree("", null));
        ret.put("expandedKeys", sysapiService.getExpandedKeys());
        ret.put("checkedKeys", sysapiService.getCheckedKeys(roleId));
        return ret;
    }

    //角色管理：保存API权限勾选结果
    @PostMapping(value = "/savekeys")
    @Operation(description = "角色管理：保存API权限勾选结果")
    public AjaxResponse savekeys(@RequestBody RoleCheckedIds roleCheckedIds) {
        sysapiService.saveCheckedKeys(
                roleCheckedIds.getRoleCode(),
                roleCheckedIds.getRoleId(),
                roleCheckedIds.getCheckedIds()
        );
        return AjaxResponse.success("保存接口权限成功!");
    }

    //接口管理：更新接口禁用状态
    @PostMapping(value = "/status/change")
    @Operation(description = "接口管理：更新接口禁用状态")
    @Parameters({
            @Parameter(name = "apiId", description = "接口ID"),
            @Parameter(name = "status", description = "接口状态"),
    })
    public AjaxResponse update(@RequestParam Long apiId,
                               @RequestParam Boolean status) {
        sysapiService.updateStatus(apiId, status);
        return AjaxResponse.success("接口禁用状态更新成功！");
    }
}
